High School Computer Science

Boolean Logic and Number Systems布尔逻辑与数制

Computers speak in binary: every piece of data (a number, a letter, an image) is ultimately a string of 0s and 1s. This guide builds the full picture from the ground up: how the binary number system works and why base-2 maps perfectly onto hardware; how hexadecimal gives programmers a compact shorthand; how to convert fluently between decimal, binary, and hex; how to add in binary; how text is encoded as ASCII and Unicode; and how Boolean logic (AND, OR, NOT) connects the world of numbers to the logic gates that make up every circuit. Truth tables appear throughout so you can reason about any logical expression by hand.计算机用二进制(binary,二进制)"说话":每一份数据(数字、字母、图像)最终都是一串 0 和 1。本指南从零开始建立完整体系:二进制数制(binary number system)如何工作,以及为何以 2 为底完美地映射到硬件;十六进制(hexadecimal,十六进制)如何给程序员提供紧凑的简写;如何在十进制(decimal,十进制)、二进制和十六进制之间流畅转换;如何做二进制加法;文本如何被编码为 ASCII 和 Unicode;以及布尔逻辑(Boolean logic,布尔逻辑),即与(AND)、或(OR)、非(NOT),如何将数字世界与构成每个电路的逻辑门(logic gate,逻辑门)连接起来。真值表(truth table,真值表)贯穿全文,使你能够手工推理任意逻辑表达式。

7 sections7 节内容 US CSTA · AP CSP · ON · BC · ABUS CSTA · AP CSP · ON · BC · AB Tables + worked conversions表格 + 例题转换

The Binary Number System二进制数制

Learning objectives学习目标
  • State what a bit and a byte are, and that n bits give 2n different patterns.
  • Convert an 8-bit binary number to decimal using a place-value table.
  • Convert a decimal number to binary by choosing place values from the largest down.
  • Calculate the largest unsigned whole number that n bits can store (2n − 1).
  • Calculate the smallest number of bits needed to store a given whole number.
  • 说明位(bit)和字节(byte)是什么,以及 n 位共有 2n 种不同组合。
  • 用位权表将 8 位二进制数转换为十进制。
  • 从最大位权开始逐个选取位权,将十进制数转换为二进制。
  • 计算 n 位能存储的最大无符号整数(2n − 1)。
  • 计算存储某个整数所需的最少位数。
Why binary? Hardware switches have two states: on (1) and off (0). Every decimal number maps to a unique sequence of 1s and 0s.为什么用二进制?硬件开关只有两种状态:开(1)和关(0)。每个十进制数都能唯一映射为一串 1 和 0。
  • Bit位(bit): a single binary digit, either 0 or 1.:单个二进制位,取值为 0 或 1。
  • Byte字节(byte): 8 bits. 1 byte can represent 28 = 256 distinct values (0–255).:8 位。1 字节可表示 28 = 256 个不同值(0–255)。
  • Place values double from right to left位权从右往左翻倍: in decimal, columns are 1, 10, 100, 1000. In binary they are 1, 2, 4, 8, 16, 32, 64, 128.:十进制中各列位权为 1、10、100、1000;二进制中为 1、2、4、8、16、32、64、128。
Key fact (AP CSP DAT-1.A, "Explain how data can be represented using bits."): Each additional bit doubles the number of possible values, so 1 bit = 2 values; 8 bits = 256 values; 16 bits = 65,536 values; 32 bits ≈ 4 billion values.关键事实(AP CSP DAT-1.A,"解释数据如何用位来表示。"):每增加一位,可表示的值的数量翻倍,所以 1 位 = 2 个值;8 位 = 256 个值;16 位 = 65,536 个值;32 位 ≈ 40 亿个值。
Worked Example 1: Binary place-value table例题 1:二进制位权表

Read the 8-bit number 10110101 and find its decimal value.读取 8 位二进制数 10110101,求其十进制值。

Bit position位位置76543210
Place value (2n)位权(2n)1286432168421
Bit位10110101
Contribution贡献值128032160401

Sum: 128 + 32 + 16 + 4 + 1 = 181.求和:128 + 32 + 16 + 4 + 1 = 181。

Each 1-bit contributes its place value; 0-bits contribute nothing. Add up only the columns where the bit is 1.每个 1 位贡献其位权;0 位不贡献任何值。只将位为 1 的列的位权相加。

Position位位置 Place value位权 Bit位 Contribution贡献值 77 128128 11 128128 66 6464 00 00 55 3232 11 3232 44 1616 11 1616 33 88 00 00 22 44 11 44 11 22 00 00 00 11 11 11 128 + 32 + 16 + 4 + 1 = 181128 + 32 + 16 + 4 + 1 = 181
Figure 1. The 8-bit number 10110101. The shaded columns hold a 1, so only their place values are added: 128 + 32 + 16 + 4 + 1 = 181. Read the sum straight from the bottom row.图 1. 8 位二进制数 10110101。阴影列的位为 1,只把这些列的位权相加:128 + 32 + 16 + 4 + 1 = 181。可直接从最下面一行读出各项。
Worked Example 2: Decimal to binary by place values, and how many bits are needed例题 2:用位权将十进制转为二进制,以及所需位数

Convert decimal 150 to 8-bit binary by choosing place values. Then state how many different values 10 bits can hold and the largest one.用位权法将十进制 150 转换为 8 位二进制。然后说明 10 位能容纳多少个不同的值,以及最大值是多少。

(a) 150 as 8-bit binary. Go down the place values from 128. If the remaining value is at least the place value, write 1 and subtract it; otherwise write 0.(a) 将 150 写成 8 位二进制。从 128 开始依次向下比较位权。若剩余值不小于该位权,则写 1 并减去它;否则写 0。

128: 150 >= 128  ->  bit 1, left 22
 64:  22 <   64  ->  bit 0, left 22
 32:  22 <   32  ->  bit 0, left 22
 16:  22 >=  16  ->  bit 1, left 6
  8:   6 <    8  ->  bit 0, left 6
  4:   6 >=   4  ->  bit 1, left 2
  2:   2 >=   2  ->  bit 1, left 0
  1:   0 <    1  ->  bit 0, left 0
Bits read left to right: 1 0 0 1 0 1 1 0  ->  10010110

Check: 128 + 16 + 4 + 2 = 150. Correct. Seven bits would only reach 127, so 150 needs 8 bits.验证:128 + 16 + 4 + 2 = 150,正确。7 位最大只能到 127,所以 150 需要 8 位。

(b) What can 10 bits hold? Each bit doubles the count, so 10 bits give 210 = 1,024 different patterns. They run from 0000000000 (0) to 1111111111, and the largest value is 210 − 1 = 1,023.(b) 10 位能容纳什么?每增加一位,数量翻倍,所以 10 位共有 210 = 1,024 种不同的组合。从 0000000000(0)到 1111111111,最大值是 210 − 1 = 1,023。

Why the largest value is one less than the count: the patterns start at 0, so 1,024 patterns cover the values 0 to 1,023.为什么最大值比总数少 1:组合从 0 开始计数,所以 1,024 种组合对应的值是 0 到 1,023。

What is the decimal value of the binary number 1010?二进制数 1010 的十进制值是多少?
§1 · Q1
44
88
1010
1212
Place values for 1010: bit 3 = 8 (1×8), bit 2 = 4 (0×4), bit 1 = 2 (1×2), bit 0 = 1 (0×1). Sum = 8 + 0 + 2 + 0 = 10.1010 各位权:第 3 位 = 8(1×8),第 2 位 = 4(0×4),第 1 位 = 2(1×2),第 0 位 = 1(0×1)。总和 = 8 + 0 + 2 + 0 = 10。
Place values in binary (right to left): 1, 2, 4, 8. For 1010: only bits at positions 3 and 1 are 1. 8 + 2 = 10.二进制位权(从右到左):1、2、4、8。对于 1010:仅第 3 位和第 1 位为 1。8 + 2 = 10。
How many distinct values can be represented with 4 bits?4 位二进制可以表示多少个不同的值?
§1 · Q2
88
1616
3232
44
n bits can represent 2n distinct values. 24 = 16 (values 0000 through 1111, i.e. decimal 0–15). Each additional bit doubles the number of possible values.n 位可表示 2n 个不同值。24 = 16(值从 0000 到 1111,即十进制 0–15)。每增加一位,可能值的数量翻倍。
n bits = 2n values. 4 bits = 24 = 16. Each bit added doubles the count.n 位 = 2n 个值。4 位 = 24 = 16。每增加一位,数量翻倍。
Exam tip考试提示

Command words such as convert, state and show ask for visible working. Draw the place-value row (128, 64, 32, 16, 8, 4, 2, 1), mark the columns that hold a 1, and only then add. Two common errors: reading the weights from left to right (1, 2, 4, ...) instead of right to left, and mixing up the number of patterns (2n) with the largest value (2n − 1).

convert(转换)、state(说明)、show(展示)这类指令词要求写出可见的步骤。先画出位权行(128、64、32、16、8、4、2、1),标出为 1 的列,再相加。两个常见错误:把位权从左到右写成 1、2、4……(应从右到左),以及把组合总数(2n)与最大值(2n − 1)混为一谈。

Check your understanding自我检测
Convert 11010011 to decimal.将 11010011 转换为十进制。

The bits that are 1 sit at positions 7, 6, 4, 1 and 0. Add their place values: 128 + 64 + 16 + 2 + 1 = 211.

为 1 的位在第 7、6、4、1、0 位。把它们的位权相加:128 + 64 + 16 + 2 + 1 = 211。

How many different values can 12 bits represent, and what is the largest unsigned value?12 位可以表示多少个不同的值?最大的无符号值是多少?

212 = 4,096 different values, running from 0 to 4,095 (212 − 1).

共有 212 = 4,096 个不同的值,范围是 0 到 4,095(212 − 1)。

What is the smallest number of bits that can store the whole number 1000? Write 1000 in binary.存储整数 1000 至少需要多少位?写出 1000 的二进制。

29 = 512 ≤ 1000 < 1024 = 210, so 9 bits are too few (largest 511) and 10 bits are needed. 1000 = 512 + 256 + 128 + 64 + 32 + 8, which is 1111101000.

29 = 512 ≤ 1000 < 1024 = 210,所以 9 位不够(最大 511),需要 10 位。1000 = 512 + 256 + 128 + 64 + 32 + 8,即 1111101000。


Hexadecimal十六进制

Learning objectives学习目标
  • State the sixteen hex digits (0 to 9, A to F), their decimal values, and why 16 = 24 gives 4 bits per hex digit.
  • Convert between hexadecimal and binary using 4-bit groups (nibbles).
  • Convert a hex number to decimal using powers of 16.
  • Convert a decimal number from 0 to 255 to a two-digit hex number.
  • Decode a hex colour code #RRGGBB into three decimal channel values and percentages.
  • 说出 16 个十六进制数字(0 到 9、A 到 F)及其十进制值,并说明为什么 16 = 24 意味着每个十六进制位对应 4 位二进制。
  • 用 4 位一组(半字节)在十六进制与二进制之间转换。
  • 用 16 的幂将十六进制数转换为十进制。
  • 将 0 到 255 的十进制数转换为两位十六进制数。
  • 把十六进制颜色代码 #RRGGBB 解码为三个十进制通道值及其百分比。
Hex (base 16) is a compact shorthand for binary: each hex digit represents exactly 4 bits (a nibble).十六进制(以 16 为底)是二进制的紧凑缩写:每个十六进制位恰好代表 4 位(半字节,nibble)。
  • Digits: 0–9 then A=10, B=11, C=12, D=13, E=14, F=15.数字:0–9,然后 A=10、B=11、C=12、D=13、E=14、F=15。
  • One byte (8 bits) = exactly 2 hex digits. e.g. 0xFF = 1111 1111 = decimal 255.一字节(8 位)= 恰好 2 个十六进制位。例如 0xFF = 1111 1111 = 十进制 255。
  • Hex prefix: 0x in code (e.g. 0x1F); # in HTML color codes (e.g. #FF0000 = pure red).十六进制前缀:代码中用 0x(如 0x1F);HTML 颜色代码中用 #(如 #FF0000 = 纯红色)。
CSTA 3A-DA-09 Descriptive Statement (verbatim): "For example, convert hexadecimal color codes to decimal percentages, ASCII/Unicode representation, and logic gates." Hex color codes are the most visible real-world use of hexadecimal.CSTA 3A-DA-09 描述性说明(原文):"例如,将十六进制颜色代码转换为十进制百分比、ASCII/Unicode 表示及逻辑门。"十六进制颜色代码是十六进制最直观的现实应用。
Worked Example 3: Hex digit table and color code例题 3:十六进制数字表与颜色代码

Show the 16 hex digits and their binary/decimal equivalents, then decode the color #3A7FBE.列出 16 个十六进制数字及其对应的二进制/十进制值,然后解码颜色代码 #3A7FBE。

Hex十六进制Decimal十进制4-bit binary4 位二进制Hex十六进制Decimal十进制4-bit binary4 位二进制
000000881000
110001991001
220010A101010
330011B111011
440100C121100
550101D131101
660110E141110
770111F151111

Decoding #3A7FBE (RGB color):解码 #3A7FBE(RGB 颜色):

#3A7FBE  →  R=0x3A  G=0x7F  B=0xBE
R: 3=0011, A=1010  →  0011 1010  =  58  (decimal)
G: 7=0111, F=1111  →  0111 1111  =  127 (decimal)
B: B=1011, E=1110  →  1011 1110  =  190 (decimal)

Each pair of hex digits is one byte (0–255). The color #3A7FBE has red=58, green=127, blue=190, a medium steel-blue.每对十六进制位代表一字节(0–255)。颜色 #3A7FBE 的红色=58、绿色=127、蓝色=190,是一种中等钢蓝色。

Worked Example 4: Hex, decimal and colour channels例题 4:十六进制、十进制与颜色通道

(a) Convert 0x2F and 0x1F4 to decimal. (b) Convert decimal 200 to hex and to binary. (c) Decode the colour #FF8000 into red, green and blue values, and write each as a percentage of 255.(a) 将 0x2F 和 0x1F4 转换为十进制。(b) 将十进制 200 转换为十六进制和二进制。(c) 将颜色 #FF8000 解码为红、绿、蓝三个值,并把每个值写成占 255 的百分比。

(a) Hex to decimal. Each hex digit is worth its value times a power of 16 (1, 16, 256, ...). The letters are A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.(a) 十六进制转十进制。每个十六进制位的值乘以 16 的幂(1、16、256……)。字母对应 A = 10、B = 11、C = 12、D = 13、E = 14、F = 15。

0x2F  = 2 x 16 + 15 x 1            = 32 + 15        = 47
0x1F4 = 1 x 256 + 15 x 16 + 4 x 1 = 256 + 240 + 4 = 500

(b) Decimal 200 to hex and binary. Divide by 16: 200 ÷ 16 = 12 remainder 8. Since 12 is C, the hex number is 0xC8. Convert each digit to a nibble: C = 1100, 8 = 1000, so the binary is 11001000.(b) 十进制 200 转十六进制和二进制。除以 16:200 ÷ 16 = 12 余 8。12 对应 C,所以十六进制是 0xC8。把每位转成半字节:C = 1100,8 = 1000,二进制为 11001000。

Check: 128 + 64 + 8 = 200. Correct.验证:128 + 64 + 8 = 200,正确。

(c) Decode the colour #FF8000. Split into pairs: FF, 80, 00 for red, green, blue.(c) 解码颜色 #FF8000。拆成三对:FF、80、00,分别是红、绿、蓝。

R = 0xFF = 15 x 16 + 15 = 255   ->  255 / 255 = 100%
G = 0x80 =  8 x 16 +  0 = 128   ->  128 / 255 = 50.2%
B = 0x00 =  0                   ->    0 / 255 = 0%

Full red, about half green and no blue make a strong orange. A hex colour is just three bytes written as three pairs of hex digits.红色全开、绿色约一半、蓝色为零,得到饱和的橙色。十六进制颜色就是三个字节,写成三对十六进制位。

What is the decimal value of the hex digit B?十六进制数字 B 的十进制值是多少?
§2 · Q1
1111
1212
1010
1616
Hex digits A=10, B=11, C=12, D=13, E=14, F=15. B = 11.十六进制数字:A=10、B=11、C=12、D=13、E=14、F=15。B = 11。
A=10, B=11, C=12, D=13, E=14, F=15. Memorise this mapping.A=10、B=11、C=12、D=13、E=14、F=15。记住这个对应关系。
How many bits does one hex digit represent?一个十六进制位代表多少位二进制?
§2 · Q2
11
22
88
44
One hex digit represents 4 bits (a nibble), because 16 = 24. Two hex digits = one byte (8 bits).一个十六进制位代表 4 位二进制(半字节),因为 16 = 24。两个十六进制位 = 一字节(8 位)。
Hex base is 16 = 24, so one hex digit encodes 4 bits. Two hex digits = 8 bits = 1 byte.十六进制底数为 16 = 24,故一个十六进制位编码 4 位二进制。两个十六进制位 = 8 位 = 1 字节。
Exam tip考试提示

For convert and decode questions, write the decimal value of every hex letter (A = 10 up to F = 15) beside the digit before you multiply, and split a colour code into three pairs before doing anything else. Common errors: treating B as 2 or as 1 (the letters stand for 10 to 15), writing 0x1F as 1 × 10 + 15 (the weight of the left digit is 16, not 10), and cutting a six-digit colour into the wrong pairs.

遇到convert(转换)和decode(解码)题,先在每个十六进制字母旁写出它的十进制值(A = 10 到 F = 15)再做乘法,颜色代码先拆成三对。常见错误:把 B 当成 2 或 1(字母代表 10 到 15);把 0x1F 算成 1 × 10 + 15(左边那位的权是 16,不是 10);把六位颜色代码拆错位置。

Check your understanding自我检测
Convert 0xB3 to decimal.将 0xB3 转换为十进制。

B = 11, so 11 × 16 + 3 = 176 + 3 = 179. (In binary this is 1011 0011.)

B = 11,所以 11 × 16 + 3 = 176 + 3 = 179。(二进制为 1011 0011。)

Write decimal 100 as a two-digit hex number.把十进制 100 写成两位十六进制数。

100 ÷ 16 = 6 remainder 4, so the hex number is 0x64. Check: 6 × 16 + 4 = 100.

100 ÷ 16 = 6 余 4,所以十六进制数是 0x64。验证:6 × 16 + 4 = 100。

Decode the colour #1A2B3C into its red, green and blue values in decimal.把颜色 #1A2B3C 解码为十进制的红、绿、蓝值。

Pairs: 1A, 2B, 3C. Red = 1 × 16 + 10 = 26. Green = 2 × 16 + 11 = 43. Blue = 3 × 16 + 12 = 60. All three are small, so the colour is very dark.

分成三对:1A、2B、3C。红 = 1 × 16 + 10 = 26;绿 = 2 × 16 + 11 = 43;蓝 = 3 × 16 + 12 = 60。三个值都很小,所以颜色很暗。


Converting Between Bases进制转换

Learning objectives学习目标
  • Convert a decimal number to binary using repeated division by 2.
  • Convert a binary number to decimal using place values.
  • Convert between binary and hex by grouping bits in fours from the right, padding the left group with zeros.
  • Explain why bit groups must start at the right-hand end.
  • Check a conversion by converting the answer back.
  • 用反复除以 2 的方法将十进制数转换为二进制。
  • 用位权将二进制数转换为十进制。
  • 从右侧起每 4 位一组,并用 0 补足最左一组,在二进制与十六进制之间转换。
  • 解释为什么分组必须从最右端开始。
  • 把结果转换回去,以检查转换是否正确。
Three conversions to master: decimal → binary, binary → decimal, binary ↔ hex.需掌握三种转换:十进制 → 二进制、二进制 → 十进制、二进制 ↔ 十六进制。
  • Binary → Decimal二进制 → 十进制: multiply each bit by its place value (1, 2, 4, 8, …) and sum.:每位乘以其位权(1、2、4、8……)后求和。
  • Decimal → Binary十进制 → 二进制: divide by 2 repeatedly; remainders (read bottom-up) are the binary digits.:反复除以 2;余数(从下往上读)即为二进制位。
  • Binary ↔ Hex二进制 ↔ 十六进制: group bits in fours from the right; look up each group in the hex table.:从右侧起每 4 位分为一组;在十六进制表中查找每组的值。
Ontario ICS3U A1.2 (verbatim): "demonstrate an understanding of how a computer uses various systems (e.g., binary, hexadecimal, ASCII, Unicode) to internally represent data." Fluent conversion is the skill this expectation describes.安大略 ICS3U A1.2(原文):"展示对计算机如何使用各种系统(如二进制、十六进制、ASCII、Unicode)在内部表示数据的理解。"流畅转换正是该期望所描述的技能。
Worked Example 5: Decimal 45 in binary and hex例题 5:十进制 45 转换为二进制和十六进制

Convert decimal 45 to (a) binary and (b) hexadecimal.将十进制 45 转换为 (a) 二进制和 (b) 十六进制。

(a) Decimal 45 → Binary (repeated division by 2).(a) 十进制 45 → 二进制(反复除以 2)。

45 ÷ 2 = 22 remainder 1   ← least significant bit (LSB)
22 ÷ 2 = 11 remainder 0
11 ÷ 2 =  5 remainder 1
 5 ÷ 2 =  2 remainder 1
 2 ÷ 2 =  1 remainder 0
 1 ÷ 2 =  0 remainder 1   ← most significant bit (MSB)

Read remainders bottom-up: 101101
Pad to 8 bits: 00101101

Check: 32 + 8 + 4 + 1 = 45. Correct.验证:32 + 8 + 4 + 1 = 45。正确。

(b) Binary 00101101 → Hex (group into nibbles).(b) 二进制 00101101 → 十六进制(分成半字节)。

0010 | 1101
  2  |  D
Result: 0x2D

Left nibble 0010 = 2; right nibble 1101 = 13 = D. So 45 decimal = 0x2D hex.左半字节 0010 = 2;右半字节 1101 = 13 = D。因此十进制 45 = 十六进制 0x2D。

Worked Example 6: Binary to hex with padding, and why grouping starts at the right例题 6:带补零的二进制转十六进制,以及为什么从右侧分组

(a) Convert the 10-bit number 1011011110 to hex and to decimal. (b) Convert 0x7A to binary and decimal. (c) Explain why the bits must be grouped from the right.(a) 将 10 位二进制数 1011011110 转换为十六进制和十进制。(b) 将 0x7A 转换为二进制和十进制。(c) 解释为什么必须从右侧对位分组。

(a) 1011011110 to hex. Group the bits in fours starting from the right, then pad the left group with zeros.(a) 将 1011011110 转为十六进制。从右侧起每 4 位分成一组,再用 0 补足最左边那一组。

1011011110  ->  10 | 1101 | 1110
pad left group:  0010 | 1101 | 1110
                    2  |   D  |   E     ->  0x2DE

Decimal value: 2 × 256 + 13 × 16 + 14 = 512 + 208 + 14 = 734. Check from the binary: 512 + 128 + 64 + 16 + 8 + 4 + 2 = 734. Correct.十进制值:2 × 256 + 13 × 16 + 14 = 512 + 208 + 14 = 734。用二进制验证:512 + 128 + 64 + 16 + 8 + 4 + 2 = 734,正确。

(b) 0x7A to binary and decimal. 7 = 0111 and A = 1010, so 0x7A = 01111010. Decimal: 7 × 16 + 10 = 122. Check: 64 + 32 + 16 + 8 + 2 = 122.(b) 将 0x7A 转为二进制和十进制。7 = 0111,A = 1010,所以 0x7A = 01111010。十进制:7 × 16 + 10 = 122。验证:64 + 32 + 16 + 8 + 2 = 122。

(c) Why start at the right. The right-most bit is the units column, and each group of four must line up with the place values 1, 2, 4, 8 (then 16, 32, 64, 128 for the next digit). Grouping from the left and padding on the right gives 1011 0111 1000 = 0xB78, which is 2,936, not 734.(c) 为什么要从右侧开始。最右边的位是个位列,每组 4 位必须与位权 1、2、4、8(下一位是 16、32、64、128)对齐。若从左侧分组并在右侧补 0,得到 1011 0111 1000 = 0xB78,即 2,936,而不是 734。

What is decimal 13 in binary?十进制 13 的二进制是什么?
§3 · Q1
1100
1101
1110
1011
13 = 8 + 4 + 1 = 1×8 + 1×4 + 0×2 + 1×1 = 1101. Or: 13÷2=6 r1, 6÷2=3 r0, 3÷2=1 r1, 1÷2=0 r1 → read up: 1101.13 = 8 + 4 + 1 = 1×8 + 1×4 + 0×2 + 1×1 = 1101。或:13÷2=6 余1,6÷2=3 余0,3÷2=1 余1,1÷2=0 余1 → 从下往上读:1101。
1100 = 12; 1110 = 14; 1011 = 11. For 13: 8+4+1=13 → bits 3,2,0 are 1 → 1101.1100 = 12;1110 = 14;1011 = 11。对于 13:8+4+1=13 → 第 3、2、0 位为 1 → 1101。
Convert binary 11110000 to hexadecimal.将二进制 11110000 转换为十六进制。
§3 · Q2
0xEF
0xFF
0xF0
0x0F
Split: 1111 | 0000. Left nibble 1111 = 15 = F. Right nibble 0000 = 0. Result: 0xF0.分组:1111 | 0000。左半字节 1111 = 15 = F。右半字节 0000 = 0。结果:0xF0。
Group 4 bits at a time: 1111=F and 0000=0. So 11110000 = 0xF0. EF would be 11101111; FF would be 11111111.每 4 位分一组:1111=F,0000=0。所以 11110000 = 0xF0。EF 对应 11101111;FF 对应 11111111。
Exam tip考试提示

For convert and show your working questions, make the method visible: the division ladder with remainders, or the groups of four bits with their hex digits. If the question fixes a width (8 bits, 12 bits), keep the leading zeros. Common errors: reading the division remainders top to bottom (they are read bottom to top), grouping bits from the left, and dropping leading zeros when the width is fixed.

遇到convert(转换)和show your working(写出步骤)类题目,要把方法写清楚:带余数的除法竖式,或四位一组及其十六进制数字。若题目规定了位数(8 位、12 位),要保留前导 0。常见错误:把除法余数从上往下读(应从下往上读)、从左侧开始分组,以及在位数固定时丢掉前导 0。

Check your understanding自我检测
Convert decimal 100 to binary using repeated division by 2.用反复除以 2 的方法把十进制 100 转换为二进制。

100 ÷ 2 = 50 r 0; 50 ÷ 2 = 25 r 0; 25 ÷ 2 = 12 r 1; 12 ÷ 2 = 6 r 0; 6 ÷ 2 = 3 r 0; 3 ÷ 2 = 1 r 1; 1 ÷ 2 = 0 r 1. Read the remainders from the bottom up: 1100100. Check: 64 + 32 + 4 = 100.

100 ÷ 2 = 50 余 0;50 ÷ 2 = 25 余 0;25 ÷ 2 = 12 余 1;12 ÷ 2 = 6 余 0;6 ÷ 2 = 3 余 0;3 ÷ 2 = 1 余 1;1 ÷ 2 = 0 余 1。从下往上读余数:1100100。验证:64 + 32 + 4 = 100。

Convert 0xC5 to 8-bit binary and to decimal.把 0xC5 转换为 8 位二进制和十进制。

C = 1100 and 5 = 0101, so the binary is 11000101. Decimal: 12 × 16 + 5 = 197.

C = 1100,5 = 0101,所以二进制是 11000101。十进制:12 × 16 + 5 = 197。

Convert the 9-bit number 110110011 to hex and to decimal.把 9 位二进制数 110110011 转换为十六进制和十进制。

Group from the right: 1 | 1011 | 0011. Pad the left group: 0001 | 1011 | 0011, which is 0x1B3. Decimal: 1 × 256 + 11 × 16 + 3 = 256 + 176 + 3 = 435.

从右侧分组:1 | 1011 | 0011。补足最左一组:0001 | 1011 | 0011,即 0x1B3。十进制:1 × 256 + 11 × 16 + 3 = 256 + 176 + 3 = 435。


Binary Arithmetic (Addition)二进制算术(加法)

Learning objectives学习目标
  • Add two binary numbers using the four addition cases and a carry row.
  • Check a binary sum by converting both numbers and the result to decimal.
  • State the range of an n-bit unsigned whole number (0 to 2n − 1).
  • Decide whether a sum overflows a given number of bits.
  • State what is stored when the carry-out bit is lost.
  • 用四种加法情形和进位行将两个二进制数相加。
  • 把两个加数和结果都转换成十进制,以检查二进制加法。
  • 说明 n 位无符号整数的范围(0 到 2n − 1)。
  • 判断一个和在给定位数下是否溢出。
  • 说明进位位丢失后实际存储的内容。
Binary addition has four cases only:二进制加法只有四种情况:
  • 0 + 0 = 0 (no carry)(无进位)
  • 0 + 1 = 1 (no carry)(无进位)
  • 1 + 0 = 1 (no carry)(无进位)
  • 1 + 1 = 10 (write 0, carry 1; exactly like decimal 9+1=10, except that it happens at 2 instead of 10)(写 0,进位 1;就像十进制中 9+1=10,只不过在 2 而不是 10 时发生)
Overflow: if the result needs more bits than are available, the leading bits are lost. This is a fundamental data-representation constraint. AP CSP DAT-1.B: "Explain the consequences of using bits to represent data." Overflow is one such consequence.溢出(overflow):如果结果需要的位数超过可用位数,最高位将丢失。这是位表示数据的根本限制。AP CSP DAT-1.B:"解释用位表示数据的后果。"溢出就是其中一种后果。
Worked Example 7: Adding binary numbers例题 7:二进制数相加

Calculate (a) 0110 + 0101 and (b) 1101 + 0111 in binary. Check against decimal.计算二进制 (a) 0110 + 0101 和 (b) 1101 + 0111。与十进制结果对照验证。

(a) 0110 + 0101 (= decimal 6 + 5 = 11).(a) 0110 + 0101(= 十进制 6 + 5 = 11)。

  0 1 1 0
+ 0 1 0 1
---------
  carry: 1 0 0 0   (carry into bits 3, 2, 1, 0)

Step by step (right to left):
  bit 0: 0+1 = 1, carry 0
  bit 1: 1+0 = 1, carry 0
  bit 2: 1+1 = 0, carry 1
  bit 3: 0+0+1(carry) = 1, carry 0

Result: 1011  (= 8+2+1 = 11 decimal) ✓

(b) 1101 + 0111 (= decimal 13 + 7 = 20).(b) 1101 + 0111(= 十进制 13 + 7 = 20)。

  1 1 0 1
+ 0 1 1 1
---------
  bit 0: 1+1 = 0, carry 1
  bit 1: 0+1+1 = 0, carry 1
  bit 2: 1+1+1 = 1, carry 1
  bit 3: 1+0+1 = 0, carry 1

Result: 1 0100  (5 bits needed)
Binary: 10100 = 16+4 = 20 decimal ✓

If restricted to 4-bit storage, the carry-out bit is lost and the result would be 0100 = 4, which is an overflow error.如果限制为 4 位存储,进位位丢失,结果将是 0100 = 4,这是一个溢出错误。

Worked Example 8: Overflow in 8-bit addition例题 8:8 位加法中的溢出

A program stores whole numbers in 8 bits (unsigned, 0 to 255). Add 200 and 100 in binary and say what is actually stored.某程序用 8 位(无符号,0 到 255)存储整数。用二进制计算 200 加 100,并说明实际存储的是什么。

Setup. 200 = 11001000 and 100 = 01100100 in 8 bits. The largest value 8 bits can hold is 28 − 1 = 255, and 200 + 100 = 300 is bigger, so overflow is expected. Add column by column from bit 0.准备。8 位下 200 = 11001000,100 = 01100100。8 位最大能表示 28 − 1 = 255,而 200 + 100 = 300 更大,所以预计会溢出。从第 0 位开始逐列相加。

bit 0: 0 + 0     = 0, carry 0
bit 1: 0 + 0     = 0, carry 0
bit 2: 0 + 1     = 1, carry 0
bit 3: 1 + 0     = 1, carry 0
bit 4: 0 + 0     = 0, carry 0
bit 5: 0 + 1     = 1, carry 0
bit 6: 1 + 1     = 0, carry 1
bit 7: 1 + 0 + 1 = 0, carry 1   <- carry out of the 8th bit

Full sum: 1 0010 1100 (9 bits) = 256 + 44 = 300

What is stored. Only 8 bits exist, so the carry-out bit has nowhere to go and the stored pattern is 00101100 = 44. The value stored is 300 − 256 = 44, not 300: this is overflow.实际存储的内容。只有 8 位,进位出来的那一位无处存放,所以存储的是 00101100 = 44。存下的值是 300 − 256 = 44,而不是 300:这就是溢出。

Python integers grow as needed and never overflow, so the code below mimics 8-bit storage by keeping only the low 8 bits.Python 的整数会按需增长,不会溢出,所以下面的代码用只保留低 8 位的方式来模拟 8 位存储。

total = 200 + 100
print(total)                        # 300: the true sum, needs 9 bits
print(total & 0xFF)                 # 44: keep only the low 8 bits
print(format(total & 0xFF, "08b"))  # 00101100
300
44
00101100

Note: the range 0 to 255 is for unsigned 8-bit numbers. The same eight bits can instead be read as signed (two's complement) numbers, which cover −128 to 127; for example 11111111 is 255 unsigned but −1 signed. A program must know which reading it uses.注意:0 到 255 的范围针对无符号 8 位数。同样的 8 位也可以按有符号(二进制补码)数解读,范围是 −128 到 127;例如 11111111 按无符号是 255,按有符号是 −1。程序必须清楚自己采用哪种解读。

What is 0011 + 0101 in binary?二进制 0011 + 0101 的结果是什么?
§4 · Q1
0110
0111
1001
1000
3 + 5 = 8. In binary: bit0: 1+1=0 carry1; bit1: 1+0+1=0 carry1; bit2: 0+1+1=0 carry1; bit3: 0+0+1=1. Result: 1000 = 8. ✓3 + 5 = 8。二进制:第 0 位:1+1=0 进 1;第 1 位:1+0+1=0 进 1;第 2 位:0+1+1=0 进 1;第 3 位:0+0+1=1。结果:1000 = 8。✓
0011=3, 0101=5, sum=8=1000 in binary. Work right to left with carries: 1+1=10, 1+0+carry=10, 0+1+carry=10, 0+0+carry=1. Result 1000.0011=3,0101=5,和=8=二进制 1000。从右到左处理进位:1+1=10,1+0+进位=10,0+1+进位=10,0+0+进位=1。结果 1000。
In binary addition, what is 1 + 1?在二进制加法中,1 + 1 等于什么?
§4 · Q2
10 (write 0, carry 1)10(写 0,进位 1)
22
1111
11
In binary, 1+1 = 2 decimal, but 2 in binary is written 10. Write the 0, carry the 1 to the next column. This is the fundamental carry rule in binary addition.在二进制中,1+1 = 十进制 2,但二进制中 2 写作 10。写下 0,将 1 进位到下一列。这是二进制加法的基本进位规则。
Binary only has 0 and 1. 1+1 exceeds 1, so like decimal 9+1 causing a carry, binary 1+1=10 (write 0, carry 1).二进制只有 0 和 1。1+1 超过 1,就像十进制 9+1 引起进位一样,二进制 1+1=10(写 0,进位 1)。
Exam tip考试提示

For add, calculate and identify questions, write the carry row above the sum and say how many bits are available before you decide on overflow. Common errors: dropping a carry between columns, giving a 5-bit answer when the question fixes 4-bit storage, and calling a sum an overflow when it still fits (11 fits in 4 bits, 17 does not).

遇到add(相加)、calculate(计算)和identify(指出)类题目,先在和的上方写出进位行,并先说明可用的位数,再判断是否溢出。常见错误:丢掉列与列之间的进位;题目规定 4 位存储却给出 5 位答案;明明放得下却说溢出(11 能放进 4 位,17 不能)。

Check your understanding自我检测
Add 0101 and 0110 in 4-bit binary. Does the sum overflow?用 4 位二进制计算 0101 加 0110。和是否溢出?

5 + 6 = 11. Columns: bit 0: 1 + 0 = 1; bit 1: 0 + 1 = 1; bit 2: 1 + 1 = 0 carry 1; bit 3: 0 + 0 + 1 = 1. Result 1011 = 11. A 4-bit number holds up to 15, so there is no overflow.

5 + 6 = 11。各列:第 0 位:1 + 0 = 1;第 1 位:0 + 1 = 1;第 2 位:1 + 1 = 0 进 1;第 3 位:0 + 0 + 1 = 1。结果 1011 = 11。4 位最大能到 15,所以没有溢出。

Add 1001 and 1000 in 4-bit storage. What is stored, and why?在 4 位存储下计算 1001 加 1000。实际存储什么?为什么?

9 + 8 = 17 = 10001, which needs 5 bits. Only 4 bits are stored, so the leading 1 is lost and 0001 (= 1) is stored. This is overflow because 17 is larger than 15.

9 + 8 = 17 = 10001,需要 5 位。只存 4 位,所以最高位的 1 丢失,存下的是 0001(= 1)。因为 17 大于 15,所以这是溢出。

What is the largest sum of two 8-bit unsigned numbers, and how many bits does it need?两个 8 位无符号数相加,最大的和是多少?需要多少位?

255 + 255 = 510 = 111111110, which needs 9 bits. An 8-bit result cannot hold it.

255 + 255 = 510 = 111111110,需要 9 位。8 位的结果放不下它。


Representing Text and Data (ASCII, Unicode, Bits and Bytes)文本与数据表示(ASCII、Unicode、位与字节)

Learning objectives学习目标
  • State that ASCII uses 7 bits for 128 characters and is usually stored in one byte per character.
  • Find the code of a character from the anchors 'A' = 65, 'a' = 97 and '0' = 48.
  • Write a character code in decimal, 8-bit binary and hex.
  • Calculate the storage for a text in bits, bytes and KB.
  • Explain why Unicode and UTF-8 exist, and how many bytes UTF-8 uses per character.
  • Distinguish the character '7' (code 55) from the number 7.
  • 说明 ASCII 用 7 位表示 128 个字符,通常每个字符存储在一个字节中。
  • 根据锚点 'A' = 65、'a' = 97、'0' = 48 求出字符的编码。
  • 把字符编码写成十进制、8 位二进制和十六进制。
  • 计算一段文本以位、字节和 KB 为单位的存储量。
  • 解释为什么需要 Unicode 和 UTF-8,以及 UTF-8 每个字符使用多少字节。
  • 区分字符 '7'(编码 55)与数字 7。
Every character is stored as a number. Two encoding standards matter:每个字符都以数字形式存储。两个编码标准至关重要:
  • ASCIIASCII: 7 bits, 128 characters (0–127). Covers A–Z, a–z, 0–9, punctuation, and control codes. 'A' = 65, 'a' = 97, '0' = 48.:7 位,128 个字符(0–127)。涵盖 A–Z、a–z、0–9、标点和控制字符。'A' = 65,'a' = 97,'0' = 48。
  • UnicodeUnicode: superset of ASCII; over 140,000 characters covering virtually every writing system (Chinese, Arabic, emoji, …). A common encoding is UTF-8 (variable 1–4 bytes; the first 128 code points are identical to ASCII).:ASCII 的超集;超过 140,000 个字符,涵盖几乎所有书写系统(中文、阿拉伯文、表情符号……)。常见编码是 UTF-8(可变 1–4 字节;前 128 个码点与 ASCII 完全相同)。
  • Bit and byte scale位与字节的换算: 1 byte = 8 bits; 1 KB = 1,024 bytes; 1 MB = 1,024 KB; 1 GB = 1,024 MB (the binary convention used in this guide; drive makers often use 1,000).:1 字节 = 8 位;1 KB = 1,024 字节;1 MB = 1,024 KB;1 GB = 1,024 MB(本指南采用的二进制约定;硬盘厂商常按 1,000 计)。
Ontario ICS3U A1.2 (verbatim): "…ASCII, Unicode…" names both encoding standards. Alberta CSE2130 outcome 1.1.2 (verbatim): "type of data; e.g., text (encoded in a format such as ASCII code), binary (encoded in binary code)."安大略 ICS3U A1.2(原文):"……ASCII、Unicode……"点名了两个编码标准。阿尔伯塔 CSE2130 结果 1.1.2(原文):"数据类型;例如,文本(以 ASCII 码等格式编码)、二进制(以二进制代码编码)。"
Worked Example 9: ASCII encoding of "Hi"例题 9:"Hi" 的 ASCII 编码

Show how the two-character string "Hi" is stored in memory using ASCII, in both decimal and binary.展示双字符字符串"Hi"如何用 ASCII 在内存中存储,包括十进制和二进制形式。

Character字符ASCII decimalASCII 十进制Binary (8-bit)二进制(8 位)Hex十六进制
'H'72010010000x48
'i'105011010010x69

Storage: 2 characters × 1 byte = 2 bytes = 16 bits total.存储:2 个字符 × 1 字节 = 2 字节 = 共 16 位。

Note: 'H' (uppercase) = 72, 'h' (lowercase) = 104. The difference is 32, which means lowercase = uppercase + 32 in ASCII, a useful pattern for case-conversion code.注意:'H'(大写)= 72,'h'(小写)= 104。差值为 32,即 ASCII 中小写 = 大写 + 32,这是大小写转换代码的有用规律。

Why Unicode extends ASCII.为什么 Unicode 扩展了 ASCII。 ASCII's 128 code points cannot represent Chinese characters, Arabic script, or emoji. Unicode assigns a unique code point to every character in every writing system. UTF-8 encodes the first 128 Unicode code points identically to ASCII (backward-compatible), then uses 2–4 bytes for the rest. The Chinese character 中 is Unicode code point U+4E2D, encoded in UTF-8 as three bytes: 0xE4 0xB8 0xAD.ASCII 的 128 个码点无法表示汉字、阿拉伯文字或表情符号。Unicode 为每种书写系统中的每个字符分配唯一码点。UTF-8 将前 128 个 Unicode 码点与 ASCII 完全相同地编码(向后兼容),其余使用 2–4 字节。汉字"中"是 Unicode 码点 U+4E2D,在 UTF-8 中编码为三字节:0xE4 0xB8 0xAD。

Worked Example 10: Encoding a short string and finding its size例题 10:对短字符串编码并求其大小

(a) Encode the string "Go!" in ASCII, giving decimal, 8-bit binary and hex for each character. (b) State its size in bytes and bits. (c) How many bytes does "Hi中" need in UTF-8? (d) Convert 'q' to uppercase using code numbers.(a) 用 ASCII 对字符串 "Go!" 编码,给出每个字符的十进制、8 位二进制和十六进制。(b) 说明它的字节数和位数。(c) "Hi中" 在 UTF-8 中需要多少字节?(d) 用编码数字把 'q' 转为大写。

(a) and (d) Encode and convert with Python. ord gives a character's code number and chr turns a number back into a character.(a) 和 (d) 用 Python 编码和转换。ord 给出字符的编码数字,chr 把数字还原为字符。

for ch in "Go!":
    print(ch, ord(ch), format(ord(ch), "08b"), format(ord(ch), "02X"))
print(len("Hi中".encode("utf-8")))
print(chr(ord("q") - 32))
G 71 01000111 47
o 111 01101111 6F
! 33 00100001 21
5
Q

So "Go!" is the codes 71, 111, 33 (binary 01000111 01101111 00100001, hex 47 6F 21).所以 "Go!" 对应的编码是 71、111、33(二进制 01000111 01101111 00100001,十六进制 47 6F 21)。

(b) Size. 3 characters × 1 byte = 3 bytes = 3 × 8 = 24 bits.(b) 大小。3 个字符 × 1 字节 = 3 字节 = 3 × 8 = 24 位。

(c) UTF-8. "Hi" uses 1 byte per letter, and 中 (U+4E2D) uses 3 bytes, so "Hi中" takes 1 + 1 + 3 = 5 bytes (the fourth line printed above).(c) UTF-8。"Hi" 每个字母 1 字节,中(U+4E2D)占 3 字节,所以 "Hi中" 共 1 + 1 + 3 = 5 字节(即上面输出的第四行)。

(d) Case conversion. 'q' has code 113; lowercase is uppercase + 32, so 113 − 32 = 81, which is 'Q' (the fifth line printed above).(d) 大小写转换。'q' 的编码是 113;小写 = 大写 + 32,所以 113 − 32 = 81,即 'Q'(上面输出的第五行)。

The ASCII code for 'A' is 65. What is the ASCII code for 'B'?字符 'A' 的 ASCII 码为 65。'B' 的 ASCII 码是多少?
§5 · Q1
6464
9797
6666
9898
ASCII codes for uppercase letters are consecutive: A=65, B=66, C=67, … Z=90. Each letter is one more than the previous.大写字母的 ASCII 码是连续的:A=65、B=66、C=67……Z=90。每个字母比前一个多 1。
A=65, so B=66 (consecutive). 97 = lowercase 'a'. 98 = lowercase 'b'. ASCII uppercase letters run 65–90.A=65,所以 B=66(连续)。97 = 小写 'a'。98 = 小写 'b'。ASCII 大写字母范围为 65–90。
Which statement about Unicode and ASCII is correct?关于 Unicode 和 ASCII,哪个说法是正确的?
§5 · Q2
Unicode replaced ASCII entirely; ASCII is no longer usedUnicode 完全取代了 ASCII;ASCII 已不再使用
Unicode is a superset of ASCII; the first 128 code points are identicalUnicode 是 ASCII 的超集;前 128 个码点完全相同
Unicode uses 7 bits per character, same as ASCIIUnicode 每个字符使用 7 位,与 ASCII 相同
ASCII can represent all characters that Unicode canASCII 可以表示 Unicode 所能表示的所有字符
Unicode is a superset of ASCII. The first 128 Unicode code points (U+0000 to U+007F) are identical to ASCII, making UTF-8 backward-compatible with ASCII text. Unicode extends far beyond ASCII to cover over 140,000 characters.Unicode 是 ASCII 的超集。前 128 个 Unicode 码点(U+0000 到 U+007F)与 ASCII 完全相同,使 UTF-8 与 ASCII 文本向后兼容。Unicode 远超 ASCII,涵盖超过 140,000 个字符。
Unicode extends ASCII (does not replace it); the first 128 code points are identical. Unicode uses variable-width encoding (1–4 bytes in UTF-8), not a fixed 7 bits.Unicode 扩展了 ASCII(而非取代);前 128 个码点完全相同。Unicode 使用可变宽度编码(UTF-8 中为 1–4 字节),而非固定 7 位。
Exam tip考试提示

For encode and state questions, use anchors instead of memorising the table: 'A' = 65, 'a' = 97, '0' = 48, then count along. Write the unit every time (3 characters = 3 bytes = 24 bits). Common errors: confusing the character '7' (code 55) with the number 7, calling ASCII a 256-character code (it defines 128 characters), and assuming every Unicode character takes one byte.

遇到encode(编码)和state(说明)类题目,不必死记整张表,用锚点推算:'A' = 65、'a' = 97、'0' = 48,再往后数。每次都写上单位(3 个字符 = 3 字节 = 24 位)。常见错误:把字符 '7'(编码 55)与数字 7 混淆;把 ASCII 说成 256 个字符的编码(它定义的是 128 个);以为每个 Unicode 字符都只占一个字节。

Check your understanding自我检测
Using 'a' = 97, find the ASCII code of 'd' in decimal and in 8-bit binary.已知 'a' = 97,求 'd' 的 ASCII 十进制编码和 8 位二进制编码。

'd' is 3 letters after 'a': 97 + 3 = 100. In binary, 100 = 64 + 32 + 4, so 01100100.

'd' 在 'a' 之后第 3 个字母:97 + 3 = 100。二进制:100 = 64 + 32 + 4,即 01100100。

What is the difference between the character '7' and the number 7 in memory?内存中字符 '7' 和数字 7 有什么区别?

The character '7' is stored as its ASCII code 55 (00110111). The number 7 is stored as 00000111. Converting a digit character to its number means subtracting the code of '0': 55 − 48 = 7.

字符 '7' 存储的是它的 ASCII 编码 55(00110111)。数字 7 存储为 00000111。把数字字符转换成数值,就是减去 '0' 的编码:55 − 48 = 7。

A plain-text file holds 2,048 ASCII characters. How many bytes and how many bits is that? Give the size in KB using 1 KB = 1,024 bytes.一个纯文本文件有 2,048 个 ASCII 字符。这是多少字节、多少位?按 1 KB = 1,024 字节给出以 KB 为单位的大小。

2,048 characters × 1 byte = 2,048 bytes = 2,048 × 8 = 16,384 bits. In KB: 2,048 ÷ 1,024 = 2 KB.

2,048 个字符 × 1 字节 = 2,048 字节 = 2,048 × 8 = 16,384 位。换成 KB:2,048 ÷ 1,024 = 2 KB。


Boolean Logic: AND, OR, NOT布尔逻辑:与、或、非

Learning objectives学习目标
  • State the output of AND, OR and NOT for every TRUE/FALSE input.
  • Apply the order NOT, then AND, then OR, and use brackets to change it.
  • Evaluate a compound Boolean expression for given values, one step at a time.
  • Translate a plain-English condition into a Boolean expression with comparison operators.
  • Use De Morgan's laws to rewrite a negated condition and confirm it with a truth table or a test case.
  • 说出 AND、OR、NOT 对每种真假输入的输出。
  • 应用先 NOT、再 AND、后 OR 的顺序,并用括号改变顺序。
  • 对给定的值逐步计算复合布尔表达式。
  • 用比较运算符把日常语言描述的条件翻译成布尔表达式。
  • 用德摩根定律改写取反的条件,并用真值表或测试用例加以确认。
Boolean values are TRUE or FALSE only. Three fundamental operators:布尔值只有 TRUE(真)或 FALSE(假)。三个基本运算符:
  • ANDAND(与): result is TRUE only when both inputs are TRUE. In code: A and B.:只有当两个输入都为 TRUE 时结果才为 TRUE。代码中:A and B。
  • OROR(或): result is TRUE when at least one input is TRUE. In code: A or B.:当至少一个输入为 TRUE 时结果为 TRUE。代码中:A or B。
  • NOTNOT(非): flips the input (NOT TRUE = FALSE, NOT FALSE = TRUE). In code: not A.:取反(NOT TRUE = FALSE,NOT FALSE = TRUE)。代码中:not A。
Ontario ICS3U A1.4 (verbatim): "demonstrate the ability to use Boolean operators (e.g., AND, OR, NOT) … correctly in computer programs." Alberta CSE1120 outcome 2.6 (verbatim): "use … Boolean … operators … to process data." BC Computer Studies 10 Content (verbatim): "…Boolean operators, comparison operators…"安大略 ICS3U A1.4(原文):"展示在计算机程序中正确使用布尔运算符(如 AND、OR、NOT)……的能力。"阿尔伯塔 CSE1120 结果 2.6(原文):"使用……布尔……运算符……来处理数据。"BC Computer Studies 10 内容(原文):"……布尔运算符、比较运算符……"
Worked Example 11: Truth tables for AND, OR, NOT例题 11:AND、OR、NOT 的真值表

Build the truth tables for all three operators and evaluate the expression (A AND B) OR (NOT C) for A=T, B=F, C=F.为三个运算符建立真值表,并求 A=T、B=F、C=F 时表达式 (A AND B) OR (NOT C) 的值。

AND truth tableAND(与)真值表

ABA AND B
TTT
TFF
FTF
FFF

OR truth tableOR(或)真值表

ABA OR B
TTT
TFT
FTT
FFF

NOT: NOT T = F; NOT F = T.NOT(非):NOT T = F;NOT F = T。

Evaluating (A AND B) OR (NOT C) with A=T, B=F, C=F:求 A=T、B=F、C=F 时 (A AND B) OR (NOT C) 的值:

Step 1: A AND B  = T AND F = F
Step 2: NOT C    = NOT F   = T
Step 3: F OR T             = T

Result: TRUE

Evaluate inside parentheses first, then combine with the outer operator, as in arithmetic. Worked Example 12 sets out the order NOT, AND, OR when there are no brackets.先求括号内的值,再与外层运算符结合,与算术中的做法相同。没有括号时的顺序(先 NOT、再 AND、后 OR)见例题 12。

Worked Example 12: Order of operations and De Morgan's law例题 12:运算顺序与德摩根定律

(a) Evaluate NOT A OR B AND C for A = FALSE, B = TRUE, C = FALSE. (b) Show what goes wrong if you work left to right. (c) Verify De Morgan's law NOT (A AND B) = (NOT A) OR (NOT B) with a truth table.(a) 求 A = FALSE、B = TRUE、C = FALSE 时 NOT A OR B AND C 的值。(b) 说明从左到右计算会出什么错。(c) 用真值表验证德摩根定律 NOT (A AND B) = (NOT A) OR (NOT B)。

(a) Order of operations. NOT is applied first, then AND, then OR (brackets override this). For A = FALSE, B = TRUE, C = FALSE:(a) 运算顺序。先算 NOT,再算 AND,最后算 OR(括号可改变顺序)。A = FALSE,B = TRUE,C = FALSE 时:

NOT A OR B AND C        A = False, B = True, C = False
Step 1 (NOT):  NOT A     = NOT False      = True
Step 2 (AND):  B AND C   = True AND False = False
Step 3 (OR):   True OR False              = True

Result: True

(b) Working left to right goes wrong. That reads the expression as ((NOT A) OR B) AND C = (True OR True) AND False = False. This is a different expression from the one written. Python follows the NOT, AND, OR order:(b) 从左到右计算会出错。那样相当于把表达式读成 ((NOT A) OR B) AND C = (True OR True) AND False = False,这是另一个表达式。Python 遵循 NOT、AND、OR 的顺序:

A, B, C = False, True, False
print(not A or B and C)        # NOT, then AND, then OR
print(((not A) or B) and C)    # left to right: a different expression
True
False

(c) De Morgan's law, checked row by row.(c) 德摩根定律,逐行验证。

ABA AND BNOT (A AND B)NOT ANOT B(NOT A) OR (NOT B)
TTTFFFF
TFFTFTT
FTFTTFT
FFFTTTT

The fourth and seventh columns match in all four rows, so NOT (A AND B) = (NOT A) OR (NOT B). The twin law NOT (A OR B) = (NOT A) AND (NOT B) checks the same way (only the row F, F gives TRUE on both sides). To negate a compound condition, negate each part and swap AND with OR.第 4 列和第 7 列在四行中完全一致,所以 NOT (A AND B) = (NOT A) OR (NOT B)。对偶定律 NOT (A OR B) = (NOT A) AND (NOT B) 的验证方法相同(只有 F、F 这一行两边都为 TRUE)。对复合条件取反时,把每个部分取反,并把 AND 与 OR 互换。

What is the result of TRUE AND FALSE?TRUE AND FALSE 的结果是什么?
§6 · Q1
TRUETRUE
It depends on the context取决于上下文
FALSEFALSE
TRUE or FALSE randomly随机为 TRUE 或 FALSE
AND requires BOTH inputs to be TRUE. Since B=FALSE, the result is FALSE regardless of A. AND is only TRUE when all inputs are TRUE.AND 要求两个输入都为 TRUE。由于 B=FALSE,无论 A 为何值,结果都是 FALSE。AND 只有在所有输入均为 TRUE 时才为 TRUE。
AND = TRUE only when BOTH inputs are TRUE. TRUE AND FALSE has one FALSE input, so the result is FALSE.AND = 只有当两个输入都为 TRUE 时才为 TRUE。TRUE AND FALSE 有一个 FALSE 输入,所以结果为 FALSE。
Evaluate: NOT (FALSE OR FALSE)求值:NOT (FALSE OR FALSE)
§6 · Q2
TRUETRUE
FALSEFALSE
Cannot be determined无法确定
ERROR错误
Step 1: FALSE OR FALSE = FALSE (OR is only TRUE when at least one input is TRUE). Step 2: NOT FALSE = TRUE.步骤 1:FALSE OR FALSE = FALSE(OR 只有至少一个输入为 TRUE 时才为 TRUE)。步骤 2:NOT FALSE = TRUE。
Inside parentheses first: FALSE OR FALSE = FALSE. Then NOT FALSE = TRUE.先求括号内:FALSE OR FALSE = FALSE。然后 NOT FALSE = TRUE。
Exam tip考试提示

For evaluate, rewrite and justify questions, write the value of each sub-expression on its own line, and add brackets even where precedence would give the same result. Common errors: working left to right (NOT binds tighter than AND, and AND tighter than OR), negating only the first part of a compound condition, and writing the negation of x > 5 as x < 5 instead of x <= 5.

遇到evaluate(求值)、rewrite(改写)和justify(论证)类题目,把每个子表达式的值各写一行,即使优先级本来就能给出相同结果也要加括号。常见错误:从左到右计算(NOT 的结合力强于 AND,AND 强于 OR);只对复合条件的第一部分取反;把 x > 5 的否定写成 x < 5,而不是 x <= 5。

Check your understanding自我检测
Evaluate NOT (TRUE AND FALSE) OR FALSE.求 NOT (TRUE AND FALSE) OR FALSE 的值。

Brackets first: TRUE AND FALSE = FALSE. Then NOT FALSE = TRUE. Finally TRUE OR FALSE = TRUE.

先算括号:TRUE AND FALSE = FALSE。然后 NOT FALSE = TRUE。最后 TRUE OR FALSE = TRUE。

Write "the user is at least 13 and under 18" as a Python condition using age, and say what it gives for age 18.用 age 把"用户至少 13 岁且未满 18 岁"写成 Python 条件,并说明 age 为 18 时的结果。

age >= 13 and age < 18. For age 18: 18 >= 13 is True but 18 < 18 is False, so the whole condition is False.

age >= 13 and age < 18。age 为 18 时:18 >= 13 为 True,但 18 < 18 为 False,所以整个条件为 False。

Use De Morgan's law to rewrite not (x > 5 and y < 3) without a leading not, then test it with x = 6, y = 3.用德摩根定律改写 not (x > 5 and y < 3),去掉最外层的 not,然后用 x = 6、y = 3 测试。

Negate each part and swap and/or: x <= 5 or y >= 3. Test x = 6, y = 3: the original gives not (True and False) = True; the rewrite gives False or True = True. They agree.

把每个部分取反并互换 and/or:x <= 5 or y >= 3。用 x = 6、y = 3 测试:原式为 not (True and False) = True;改写后为 False or True = True。两者一致。

Going deeper: Boolean expressions in programming conditions深入:编程条件中的布尔表达式

In code, Boolean expressions appear inside if statements and while loops. Compound conditions combine comparison operators with Boolean operators: e.g. if (age >= 16 and has_license). De Morgan's Laws are useful for simplification: NOT (A AND B) = (NOT A) OR (NOT B); NOT (A OR B) = (NOT A) AND (NOT B). These let you rewrite negated compound conditions into a form that is easier to read and test.在代码中,布尔表达式出现在 if 语句和 while 循环中。复合条件将比较运算符与布尔运算符结合:例如 if (age >= 16 and has_license)。德摩根定律对化简很有用:NOT (A AND B) = (NOT A) OR (NOT B);NOT (A OR B) = (NOT A) AND (NOT B)。这些定律使你能够将取反的复合条件改写为更易读、更易测试的形式。


Logic Gates and Truth Tables逻辑门与真值表

Learning objectives学习目标
  • State the output of AND, OR and NOT gates for every input.
  • Recognise the standard gate shapes: AND (flat back), OR (curved back, pointed front), NOT (triangle with a small circle).
  • Build a truth table with 2n rows and one intermediate column per gate.
  • Read a simple circuit diagram as a Boolean expression, and draw the circuit for an expression.
  • Relate a half adder to binary addition of two single bits.
  • 说出 AND、OR、NOT 门对每种输入的输出。
  • 认出标准门符号:AND(平背)、OR(弧形背、尖头)、NOT(三角形加小圆圈)。
  • 建立有 2n 行的真值表,每个门一个中间列。
  • 把简单的电路图读成布尔表达式,并为表达式画出电路。
  • 把半加器与两个单独位的二进制加法联系起来。
A logic gate is a hardware circuit that implements one Boolean operation. Three fundamental gates correspond to the three Boolean operators:逻辑门(logic gate)是实现一个布尔运算的硬件电路。三个基本门对应三个布尔运算符:
  • AND gateAND 门(与门): two inputs; output is 1 only when both inputs are 1.:两个输入;只有当两个输入都为 1 时输出为 1。
  • OR gateOR 门(或门): two inputs; output is 1 when at least one input is 1.:两个输入;至少一个输入为 1 时输出为 1。
  • NOT gate / inverterNOT 门 / 反相器(非门): one input; output is the complement (0→1, 1→0).:一个输入;输出为补(0→1,1→0)。
CSTA 3B-CS-02 (verbatim): "Illustrate ways computing systems implement logic, input, and output through hardware components." Descriptive Statement: "Examples of components could include logic gates and IO pins." CSTA 3A-DA-09 Descriptive Statement explicitly names "logic gates" as a conversion example.CSTA 3B-CS-02(原文):"说明计算系统如何通过硬件组件实现逻辑、输入和输出。"描述性说明:"组件示例包括逻辑门和 IO 引脚。"CSTA 3A-DA-09 描述性说明明确将"逻辑门"列为转换示例。
Worked Example 13: Combined gate truth table例题 13:组合门真值表

Build the truth table for the circuit: Output = (A AND B) OR (NOT A). Use 0/1 notation.为以下电路建立真值表:输出 = (A AND B) OR (NOT A)。使用 0/1 表示法。

ABA AND BNOT AOutput: (A AND B) OR (NOT A)输出:(A AND B) OR (NOT A)
00011
01011
10000
11101

Reading the table: whenever A=0, NOT A=1, so the OR is always 1. When A=1, NOT A=0, so the output equals A AND B = B. The circuit behaves like a conditional: "give me B, but only when A is 1; otherwise output 1."读表:只要 A=0,NOT A=1,则 OR 结果始终为 1。当 A=1 时,NOT A=0,输出等于 A AND B = B。该电路的行为类似于条件:当 A=1 时输出 B,否则输出 1。

How to build any truth table:如何构建任意真值表:

1. List all input combinations (n inputs = 2^n rows).
   2 inputs → 4 rows: 00, 01, 10, 11
   3 inputs → 8 rows: 000, 001, 010, 011, 100, 101, 110, 111

2. Add intermediate columns for each sub-expression.

3. Fill the final output column by evaluating each row.
AA BB AND与 OR或 NOT非 QQ A AND BA AND B NOT ANOT A AABBQQ 000011 001111 110000 111111
Figure 2. The circuit for Output = (A AND B) OR (NOT A), the same expression as Worked Example 13. Shapes: AND has a flat back, OR has a curved back and pointed front, NOT is a triangle with a small circle. The truth table on the right matches the one in Worked Example 13: Q = 1, 1, 0, 1 for AB = 00, 01, 10, 11.图 2. 输出 = (A AND B) OR (NOT A) 的电路,与例题 13 的表达式相同。符号形状:AND 为平背,OR 为弧形背加尖头,NOT 为带小圆圈的三角形。右侧真值表与例题 13 一致:AB = 00、01、10、11 时 Q = 1、1、0、1。
Worked Example 14: A half adder links gates to binary addition例题 14:半加器把逻辑门与二进制加法联系起来

(a) Build the truth table for S = (A OR B) AND (NOT (A AND B)) and C = A AND B. (b) Compare it with adding two single bits in binary. (c) How many rows does a 3-input truth table have, and for how many is Q = (A OR B) AND (NOT C) equal to 1?(a) 建立 S = (A OR B) AND (NOT (A AND B)) 和 C = A AND B 的真值表。(b) 与两个单独位的二进制加法进行比较。(c) 3 个输入的真值表有多少行?其中 Q = (A OR B) AND (NOT C) 为 1 的有多少行?

(a) Truth table. The circuit has two outputs: S = (A OR B) AND (NOT (A AND B)) and C = A AND B. Fill one column per gate.(a) 真值表。该电路有两个输出:S = (A OR B) AND (NOT (A AND B)) 和 C = A AND B。每个门一列。

ABA OR BA AND BNOT (A AND B)SC
0000100
0110110
1010110
1111001

(b) Link to binary addition. Add two single bits: 0 + 0 = 00, 0 + 1 = 01, 1 + 0 = 01, 1 + 1 = 10. In every row the carry bit is C and the sum bit is S. S is the "exactly one input is 1" function (XOR) and C is AND. This circuit is called a half adder; chaining adders and passing each carry along adds numbers of any width.(b) 与二进制加法的联系。把两个单独的位相加:0 + 0 = 00,0 + 1 = 01,1 + 0 = 01,1 + 1 = 10。每一行中进位位就是 C,和位就是 S。S 是"恰好有一个输入为 1"的函数(XOR),C 就是 AND。这个电路叫做半加器;把多个加法器串联并传递每一级的进位,就可以对任意位数的数相加。

(c) Counting rows. A circuit with 3 inputs has 23 = 8 rows. For Q = (A OR B) AND (NOT C), Q is 1 only when C = 0 and at least one of A, B is 1, which happens for (A, B, C) = (0,1,0), (1,0,0) and (1,1,0): 3 of the 8 rows.(c) 数行数。3 个输入的电路有 23 = 8 行。对于 Q = (A OR B) AND (NOT C),只有当 C = 0 且 A、B 至少一个为 1 时 Q 才为 1,即 (A, B, C) = (0,1,0)、(1,0,0)、(1,1,0),共 8 行中的 3 行。

What is the output of an AND gate when one input is 0 and the other is 1?当 AND 门的一个输入为 0、另一个为 1 时,输出是什么?
§7 · Q1
11
00
Depends on which input is 0取决于哪个输入为 0
Undefined未定义
AND gate output is 1 only when BOTH inputs are 1. If either input is 0, the output is 0. Here one input is 0, so output = 0.AND 门只有当两个输入都为 1 时输出才为 1。如果任一输入为 0,输出为 0。此处一个输入为 0,所以输出 = 0。
AND requires BOTH inputs to be 1 for output=1. One input is 0, so output=0 regardless of which is which.AND 要求两个输入都为 1 才能输出 1。一个输入为 0,所以无论哪个是 0,输出都为 0。
How many rows does the truth table for a 3-input Boolean expression have?一个 3 输入布尔表达式的真值表有多少行?
§7 · Q2
33
66
44
88
n inputs produce 2n rows (all combinations of T/F). 3 inputs: 23 = 8 rows (000, 001, 010, 011, 100, 101, 110, 111).n 个输入产生 2n 行(T/F 的所有组合)。3 个输入:23 = 8 行(000、001、010、011、100、101、110、111)。
Truth table rows = 2n where n = number of inputs. 3 inputs = 23 = 8 rows. 2 inputs = 4 rows; 4 inputs = 16 rows.真值表行数 = 2n,其中 n = 输入数量。3 个输入 = 23 = 8 行。2 个输入 = 4 行;4 个输入 = 16 行。
Exam tip考试提示

For draw, complete and state questions, label every gate output with its sub-expression and fill the truth table one gate column at a time. List inputs in counting order (00, 01, 10, 11) so no row is skipped. Common errors: leaving out the small circle that marks NOT, drawing an OR gate with a flat back like AND, and treating 1 OR 1 as 10 (a gate output is a single bit, so 1 OR 1 = 1).

遇到draw(画出)、complete(补全)和state(说明)类题目,给每个门的输出标上子表达式,并一次填一个门的列。输入按计数顺序(00、01、10、11)列出,避免漏行。常见错误:漏画表示 NOT 的小圆圈;把 OR 门画成和 AND 一样的平背;把 1 OR 1 当成 10(门的输出只有一位,所以 1 OR 1 = 1)。

Check your understanding自我检测
What does an OR gate output for inputs 0 and 1, and what does a NOT gate output for input 1?输入为 0 和 1 时 OR 门输出什么?输入为 1 时 NOT 门输出什么?

OR outputs 1 (at least one input is 1). NOT outputs 0 (it flips its input).

OR 门输出 1(至少一个输入为 1)。NOT 门输出 0(它把输入取反)。

How many rows does the truth table of a 4-input circuit have? And of a 5-input circuit?4 个输入的电路的真值表有多少行?5 个输入的呢?

24 = 16 rows and 25 = 32 rows. Each extra input doubles the number of rows.

24 = 16 行,25 = 32 行。每多一个输入,行数翻倍。

Use the half adder to add the single bits 1 and 1. What are S and C, and which two-bit number do they form?用半加器计算单独的位 1 加 1。S 和 C 分别是多少?它们组成的两位数是多少?

S = (1 OR 1) AND (NOT (1 AND 1)) = 1 AND 0 = 0, and C = 1 AND 1 = 1. Written as C then S, the result is 10 in binary, which is 2.

S = (1 OR 1) AND (NOT (1 AND 1)) = 1 AND 0 = 0,C = 1 AND 1 = 1。按 C、S 的顺序写出,结果是二进制 10,即 2。

Going deeper: NAND, NOR, and universal gates深入:NAND、NOR 与通用门

NAND (NOT AND) and NOR (NOT OR) are each universal gates: any Boolean function can be built from NAND gates alone (or NOR gates alone). This is one reason NAND is a common building block in real chips: it is cheap to fabricate in CMOS. NAND truth table: output is 0 only when both inputs are 1 (the complement of AND). XOR (exclusive OR) outputs 1 when exactly one input is 1 (not both). It is used in binary addition circuits (the sum bit of a half adder is XOR, the carry bit is AND).NAND(NOT AND)和 NOR(NOT OR)各自都是通用门:任何布尔函数都可以仅用 NAND 门(或仅用 NOR 门)构建。这是 NAND 门成为真实芯片中常见基础单元的原因之一:它在 CMOS 工艺中制造成本低。NAND 真值表:只有当两个输入都为 1 时输出才为 0(AND 的补)。XOR(异或)在恰好一个输入为 1(而非两个都是)时输出 1。它用于二进制加法电路(半加器的和位是 XOR,进位位是 AND)。


Exam Strategy and Common Pitfalls考试策略与常见陷阱

Number system questions (§1–§4)数制题(§1–§4)
  • Always show the place-value table.始终列出位权表。 For binary-to-decimal, write out the column headers (128, 64, 32, 16, 8, 4, 2, 1) and fill in the bits. Visible working lets you find your own slip.对于二进制转十进制,写出列标题(128、64、32、16、8、4、2、1)并填入位值。清晰的步骤能让你找出自己的笔误。
  • Hex ↔ binary: go via nibbles.十六进制 ↔ 二进制:通过半字节转换。 Never convert hex to decimal then to binary. Split hex digits into 4-bit nibbles directly. 0xB3 = 1011 0011, not "179 in decimal first."不要将十六进制先转十进制再转二进制。直接将十六进制位拆成 4 位半字节。0xB3 = 1011 0011,而不是"先转为十进制 179"。
  • Binary addition: track carries explicitly.二进制加法:明确追踪进位。 Write the carry row above the addition. Forgetting to propagate a carry is the most common arithmetic error. Check by converting both operands to decimal, adding, then converting back.在加法上方写出进位行。忘记传播进位是最常见的算术错误。通过将两个操作数转换为十进制相加后再转回来验证。
Text encoding questions (§5)文本编码题(§5)
  • Know the key ASCII anchors.记住关键 ASCII 锚点。 'A'=65, 'a'=97, '0'=48. From these, any letter or digit offset is computable: 'C' = 65+2 = 67; 'z' = 97+25 = 122.'A'=65,'a'=97,'0'=48。从这些锚点出发,任何字母或数字的偏移量都可以计算:'C' = 65+2 = 67;'z' = 97+25 = 122。
  • Unicode ≠ ASCII for non-Latin characters.非拉丁字符中 Unicode ≠ ASCII。 ASCII cannot encode Chinese, Arabic, or emoji. State this explicitly when a question involves non-English text.ASCII 无法编码中文、阿拉伯文或表情符号。当题目涉及非英文文本时,请明确说明这一点。
Boolean logic and gates (§6–§7)布尔逻辑与逻辑门(§6–§7)
  • Build the truth table column by column.逐列构建真值表。 For a compound expression, add one intermediate column per operator. Never try to evaluate the whole expression in your head per row; evaluate sub-expressions first.对于复合表达式,每个运算符增加一个中间列。不要试图在脑子里逐行求整个表达式;先求子表达式。
  • AND vs OR: the decisive test.AND 与 OR 的决定性测试。 AND is FALSE unless all inputs are TRUE. OR is TRUE unless all inputs are FALSE. Test with one TRUE and one FALSE input: AND gives FALSE and OR gives TRUE, so that one row tells them apart. (The all-FALSE and all-TRUE rows give the same output for both.)除非所有输入都为 TRUE,否则 AND 为 FALSE。除非所有输入都为 FALSE,否则 OR 为 TRUE。用一个 TRUE、一个 FALSE 的输入来测试:AND 得 FALSE,OR 得 TRUE,这一行就能区分二者。(全 FALSE 和全 TRUE 这两行,AND 与 OR 的输出相同。)
  • n inputs = 2n rows.n 个输入 = 2n 行。 Always check that your truth table has the right number of rows before filling it in.在填写真值表之前,始终检查行数是否正确。
Answer hygiene作答规范
  • Use 0 and 1, not T and F, for gate questions.逻辑门题中用 0 和 1,而非 T 和 F。 Hardware and circuit questions conventionally use 0/1. Logical expressions in code use TRUE/FALSE. Use the notation the question uses.硬件和电路题习惯用 0/1。代码中的逻辑表达式用 TRUE/FALSE。使用题目所用的表示法。
  • Verify conversions in both directions.双向验证转换。 After converting decimal 45 to binary 00101101, always verify: 32+8+4+1=45. This 10-second check catches arithmetic slips.将十进制 45 转换为二进制 00101101 后,始终验证:32+8+4+1=45。这个 10 秒检查能发现算术失误。

Flashcards闪卡

0 / 14 flipped0 / 14 已翻
What is a bit?什么是位(bit)?
A single binary digit: 0 or 1. The smallest unit of digital information. AP CSP DAT-1.A: "Explain how data can be represented using bits."单个二进制位:0 或 1。数字信息的最小单位。AP CSP DAT-1.A:"解释数据如何用位来表示。"
How many values can n bits represent?n 位可以表示多少个值?
2n values. 1 bit = 2 values; 8 bits = 256 values; 16 bits = 65,536 values. Each additional bit doubles the count.2n 个值。1 位 = 2 个值;8 位 = 256 个值;16 位 = 65,536 个值。每增加一位,数量翻倍。
Binary place values (right to left)二进制位权(从右到左)
1, 2, 4, 8, 16, 32, 64, 128, … Each column is double the one to its right.1、2、4、8、16、32、64、128……每列是其右列的两倍。
Decimal 45 in binary?十进制 45 的二进制是什么?
00101101. Check: 32+8+4+1 = 45. Method: divide repeatedly by 2, read remainders bottom-up.00101101。验证:32+8+4+1 = 45。方法:反复除以 2,从下往上读余数。
Hex digits A–F in decimal十六进制 A–F 的十进制值
A=10, B=11, C=12, D=13, E=14, F=15. One hex digit = 4 bits. Two hex digits = 1 byte.A=10、B=11、C=12、D=13、E=14、F=15。一个十六进制位 = 4 位。两个十六进制位 = 1 字节。
Convert 0xB3 to binary将 0xB3 转换为二进制
B=1011, 3=0011 → 10110011. Group 4 bits per hex digit, left to right.B=1011,3=0011 → 10110011。每个十六进制位对应 4 位,从左到右。
Binary addition: 1 + 1 = ?二进制加法:1 + 1 = ?
10 (write 0, carry 1). Binary has no digit "2": exceeding 1 causes a carry, just like exceeding 9 causes a carry in decimal.10(写 0,进位 1)。二进制没有数字"2":超过 1 就会进位,就像十进制超过 9 进位一样。
What is overflow?什么是溢出(overflow)?
When a result needs more bits than are available; the extra leading bits are lost, giving a wrong answer. Overflow is a consequence of using a fixed number of bits to represent data (AP CSP DAT-1.B).当结果需要的位数超过可用位数时,多余的高位丢失,导致错误答案。溢出是用固定位数表示数据的一种后果(AP CSP DAT-1.B)。
ASCII: 'A', 'a', '0' valuesASCII:'A'、'a'、'0' 的值
'A' = 65, 'a' = 97, '0' = 48. Lowercase = uppercase + 32. Letters are consecutive (B=66, C=67…).'A' = 65,'a' = 97,'0' = 48。小写 = 大写 + 32。字母连续(B=66、C=67……)。
ASCII vs UnicodeASCII 与 Unicode
ASCII: 7-bit, 128 characters, English only. Unicode: superset of ASCII, 140,000+ characters, every writing system. UTF-8 is backward-compatible with ASCII. ON ICS3U A1.2.ASCII:7 位,128 个字符,仅限英文。Unicode:ASCII 的超集,140,000+ 个字符,涵盖所有书写系统。UTF-8 与 ASCII 向后兼容。ON ICS3U A1.2。
AND operator: truth table summaryAND 运算符(与):真值表总结
TRUE only when BOTH inputs are TRUE. All other cases: FALSE. "Both must be on."只有两个输入都为 TRUE 时才为 TRUE。其他情况均为 FALSE。"两个都必须开启。"
OR operator: truth table summaryOR 运算符(或):真值表总结
FALSE only when BOTH inputs are FALSE. All other cases: TRUE. "At least one must be on."只有两个输入都为 FALSE 时才为 FALSE。其他情况均为 TRUE。"至少一个开启即可。"
NOT operatorNOT 运算符(非)
Flips the input: NOT TRUE = FALSE; NOT FALSE = TRUE. Single input, single output. Inverts the signal.取反:NOT TRUE = FALSE;NOT FALSE = TRUE。单输入、单输出。反转信号。
How many truth table rows for n inputs?n 个输入的真值表有多少行?
2n rows. 2 inputs = 4 rows; 3 inputs = 8 rows; 4 inputs = 16 rows.2n 行。2 个输入 = 4 行;3 个输入 = 8 行;4 个输入 = 16 行。

Practice Quiz综合测验

What is the decimal value of the binary number 11001010?二进制数 11001010 的十进制值是多少?
Q1
194194
186186
200200
202202
Place values for 11001010: 128+64+0+0+8+0+2+0 = 128+64+8+2 = 202.11001010 各位权:128+64+0+0+8+0+2+0 = 128+64+8+2 = 202。
Write out column values (128,64,32,16,8,4,2,1) under each bit: 1,1,0,0,1,0,1,0. Add where bit=1: 128+64+8+2=202.在每位下方写出位权(128、64、32、16、8、4、2、1):1、1、0、0、1、0、1、0。位为 1 处相加:128+64+8+2=202。
What is the hexadecimal representation of binary 10101111?二进制 10101111 的十六进制表示是什么?
Q2
0xBA
0xAF
0xFA
0xAB
Split: 1010 | 1111. Left nibble 1010 = 10 = A. Right nibble 1111 = 15 = F. Result: 0xAF.分组:1010 | 1111。左半字节 1010 = 10 = A。右半字节 1111 = 15 = F。结果:0xAF。
Group 4 bits: 1010=A (=10), 1111=F (=15). So 10101111 = 0xAF, not 0xAB or 0xFA.每 4 位分组:1010=A(=10),1111=F(=15)。所以 10101111 = 0xAF,不是 0xAB 或 0xFA。
What is the result of 0111 + 0101 in binary?二进制 0111 + 0101 的结果是什么?
Q3
1010
1011
1100
1110
7 + 5 = 12. In binary: bit0: 1+1=0 c1; bit1: 1+0+1=0 c1; bit2: 1+1+1=1 c1; bit3: 0+0+1=1. Result: 1100 = 12. ✓7 + 5 = 12。二进制:第 0 位:1+1=0 进 1;第 1 位:1+0+1=0 进 1;第 2 位:1+1+1=1 进 1;第 3 位:0+0+1=1。结果:1100 = 12。✓
0111=7, 0101=5, sum=12=1100. Work right-to-left tracking carries carefully.0111=7,0101=5,和=12=1100。从右到左仔细追踪进位。
The ASCII code for 'Z' is 90. What is the ASCII code for 'z' (lowercase)?字符 'Z' 的 ASCII 码为 90。小写字母 'z' 的 ASCII 码是多少?
Q4
122122
9191
116116
5858
Lowercase = uppercase + 32. 'Z' = 90, so 'z' = 90 + 32 = 122. The same +32 offset applies to all letter pairs A/a through Z/z.小写 = 大写 + 32。'Z' = 90,所以 'z' = 90 + 32 = 122。同样的 +32 偏移量适用于所有字母对 A/a 到 Z/z。
Lowercase letters = uppercase + 32. 'a'=97='A'+32=65+32. So 'z'='Z'+32=90+32=122.小写字母 = 大写 + 32。'a'=97='A'+32=65+32。所以 'z'='Z'+32=90+32=122。
Evaluate: (TRUE OR FALSE) AND (NOT TRUE)求值:(TRUE OR FALSE) AND (NOT TRUE)
Q5
TRUETRUE
FALSEFALSE
Cannot be determined无法确定
TRUE AND FALSETRUE AND FALSE
Step 1: TRUE OR FALSE = TRUE. Step 2: NOT TRUE = FALSE. Step 3: TRUE AND FALSE = FALSE.步骤 1:TRUE OR FALSE = TRUE。步骤 2:NOT TRUE = FALSE。步骤 3:TRUE AND FALSE = FALSE。
Left bracket: TRUE OR FALSE = TRUE. Right: NOT TRUE = FALSE. Then TRUE AND FALSE = FALSE.左括号:TRUE OR FALSE = TRUE。右边:NOT TRUE = FALSE。然后 TRUE AND FALSE = FALSE。
Which CSTA standard names "ASCII/Unicode representation" and "logic gates" as examples of bit representation? 🇺🇸 CSTA哪个 CSTA 标准将"ASCII/Unicode 表示"和"逻辑门"列为位表示的示例?🇺🇸 CSTA
Q6
3B-CS-02
3A-AP-17
3A-DA-09
DAT-1.B
CSTA 3A-DA-09 (verbatim): "Translate between different bit representations of real-world phenomena, such as characters, numbers, and images." Its Descriptive Statement names "ASCII/Unicode representation, and logic gates" explicitly.CSTA 3A-DA-09(原文):"在不同位表示形式(如字符、数字和图像)之间转换。"其描述性说明明确点名"ASCII/Unicode 表示及逻辑门"。
3A-DA-09 is the data-representation standard that names ASCII, Unicode, and logic gates. 3B-CS-02 covers hardware implementation of logic. DAT-1.B is the AP CSP Learning Objective (consequence of bit representation).3A-DA-09 是点名 ASCII、Unicode 和逻辑门的数据表示标准。3B-CS-02 涵盖逻辑的硬件实现。DAT-1.B 是 AP CSP 学习目标(位表示的后果)。
Ontario ICS3U expectation A1.4 requires students to correctly use which operators? 🇨🇦 ON ICS3U安大略 ICS3U 期望 A1.4 要求学生正确使用哪些运算符?🇨🇦 ON ICS3U
Q7
Boolean operators (AND, OR, NOT), comparison operators, and arithmetic operators布尔运算符(AND、OR、NOT)、比较运算符和算术运算符
Only Boolean operators (AND, OR, NOT)仅布尔运算符(AND、OR、NOT)
Only comparison operators仅比较运算符
Bitwise operators only仅位运算符
ICS3U A1.4 (verbatim): "demonstrate the ability to use Boolean operators (e.g., AND, OR, NOT), comparison operators (i.e., equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to), arithmetic operators … and order of operations correctly in computer programs."ICS3U A1.4(原文):"展示在计算机程序中正确使用布尔运算符(如 AND、OR、NOT)、比较运算符(即等于、不等于、大于、小于、大于等于、小于等于)、算术运算符……及运算顺序的能力。"
A1.4 names all three categories: Boolean operators, comparison operators, AND arithmetic operators. See the verbatim expectation text.A1.4 点名三类:布尔运算符、比较运算符和算术运算符。见原文期望文本。

Readiness Checklist准备就绪清单

Tick each item when you can do it cold, without notes, on a first attempt.能在无笔记、首次尝试下完成,再勾选每一项。

0 / 11 mastered已掌握 0 / 11

What This Feeds Into本单元的去向

Binary representation, Boolean logic, and data encoding underpin nearly every other topic in computer science. Control flow uses Boolean expressions in every if-statement and while-loop. Data structures store values using bit-level representations. Networks transmit binary data encoded in bytes. Understanding this unit makes every subsequent unit more legible because you will know what the hardware is actually doing beneath the abstraction layers.二进制表示、布尔逻辑和数据编码几乎支撑着计算机科学中的每个其他主题。控制流在每个 if 语句和 while 循环中使用布尔表达式。数据结构使用位级表示在内存中存储值。网络以字节编码的二进制数据进行传输。理解本单元会使每个后续单元更加清晰,因为你将了解硬件在抽象层之下实际在做什么。

Within High School Computer Science.在 HS Computer Science 内部。

Programming Fundamentals (Unit 2) uses Boolean data types and operators directly in code; the truth tables here give you the exact semantics. Control Flow (Unit 3) builds compound conditions from Boolean operators. Strings and Text Processing (Unit 6) depends on character encoding (ASCII / Unicode) established here. Networks and the Internet (Unit 11) transmits binary data in packets; bytes, bits, and hex addresses are the vocabulary of that unit.编程基础(单元 2)在代码中直接使用布尔数据类型和运算符;这里的真值表给出了确切的语义。控制流(单元 3)从布尔运算符构建复合条件。字符串与文本处理(单元 6)依赖于此处建立的字符编码(ASCII / Unicode)。网络与互联网(单元 11)以数据包传输二进制数据;字节、位和十六进制地址是该单元的词汇。

AP feeder.AP 衔接。

AP CSP Big Idea 2 (Data) covers the number-representation topics in §1–§5, and its Learning Objectives DAT-1.A and DAT-1.B name the same ideas. AP CSA (Java) uses boolean as a primitive type with operators &&, ||, !, which are the same AND/OR/NOT under Java syntax. Fluency with truth tables from this guide transfers directly to Java boolean expressions.AP CSP 大概念 2(数据)涵盖 §1–§5 中的数制表示主题,其学习目标 DAT-1.A 和 DAT-1.B 点名的正是这些概念。AP CSA(Java)将 boolean 用作原始类型,运算符为 &&、||、!,即 Java 语法下的 AND/OR/NOT。本指南的真值表熟练度可直接迁移到 Java 布尔表达式。

This is the ladder we coach students up: from these high school foundations into AP and IB, and on to first-year university. If you would rather have a guide for the next step than take it on trust, Dingrui Scholars tutors teach exactly this path in one-on-one sessions and small-group classes. A free assessment is the place to start.这正是我们陪伴学生攀登的阶梯:从这些高中基础,迈入 AP 与 IB,再走向大学一年级。如果你希望在下一步有人引路,而不是凭感觉前行,Dingrui Scholars 的导师正是沿着这条路径,以一对一辅导与小班课授课。欢迎从免费评估开始。

Common Questions常见问题

How do you convert a decimal number to binary?如何把十进制数转换成二进制?

Divide the decimal number by 2 repeatedly, recording each remainder, then read the remainders from bottom to top to get the binary digits. To convert back, multiply each bit by its place value (1, 2, 4, 8, and so on) and add them up.把十进制数反复除以 2 并记录每次的余数,然后从下往上读这些余数就得到二进制位。反向转换时,把每一位乘以它的位值(1、2、4、8 等)再相加即可。

What is the difference between ASCII and Unicode?ASCII 和 Unicode 有什么区别?

ASCII uses 7 bits to represent 128 characters covering English letters, digits, punctuation, and control codes, while Unicode is a superset with over 140,000 characters covering virtually every writing system, including Chinese and emoji. Its first 128 code points are identical to ASCII.ASCII 用 7 位表示 128 个字符,涵盖英文字母、数字、标点和控制码;Unicode 是它的超集,拥有超过 14 万个字符,几乎涵盖所有书写系统,包括中文和表情符号。它的前 128 个码点与 ASCII 完全相同。

What do the Boolean operators AND, OR, and NOT do?布尔运算符 AND、OR、NOT 各做什么?

AND is True only when both inputs are True, OR is True when at least one input is True, and NOT flips its single input so True becomes False and False becomes True. A truth table lists the output for every possible combination of inputs.AND 只有在两个输入都为 True 时才为 True,OR 在至少一个输入为 True 时为 True,NOT 翻转它唯一的输入,使 True 变 False、False 变 True。真值表列出每种输入组合对应的输出。

Syllabus alignment & how to use this guide大纲对照与使用说明

How to use this guide如何使用本指南

Boolean logic and number systems appear in all four curricula, though with varying depth. Ontario ICS3U is the most explicit: A1.2 names binary, hexadecimal, ASCII, and Unicode verbatim; A1.4 names AND, OR, NOT verbatim. US CSTA 3A-DA-09 links bit representation to characters, numbers, images, and explicitly mentions logic gates in its Descriptive Statement. AP CSP Big Idea 2 (Data) covers binary numbers and bit representation (topics 2.1–2.2). BC names Boolean operators in Computer Studies 10 but has no explicit binary/hex conversion standard. Alberta names Boolean operators in CSE1120 and ASCII/binary encoding in CSE2130 but has no dedicated number-systems module. The table below maps each section to your curriculum.布尔逻辑和数制在四套课程中均有出现,但深度各异。安大略 ICS3U 最为明确:A1.2 原文点名二进制、十六进制、ASCII 和 Unicode;A1.4 原文点名 AND、OR、NOT。美国 CSTA 3A-DA-09 将位表示与字符、数字、图像关联,其描述性说明明确提及逻辑门。AP CSP 大概念 2(数据)涵盖二进制数和位表示(主题 2.1–2.2)。BC 在 Computer Studies 10 中命名了布尔运算符,但无明确的二进制/十六进制转换标准。阿尔伯塔在 CSE1120 中命名了布尔运算符、在 CSE2130 中命名了 ASCII/二进制编码,但无专门的数制模块。下表将每节内容映射到你的课程。

If you are in…如果你在… Focus on these sections重点学习 Defer / lighter可推迟 / 减负 Source依据
🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP §1–§5 (binary, hex, conversions, ASCII/Unicode, bit/byte) are the core of AP CSP Big Idea 2 (DAT-1.A, DAT-1.B). §6–§7 (Boolean logic, gates) map to CSTA 3A-DA-09 Descriptive Statement and 3B-CS-02.§1–§5(二进制、十六进制、转换、ASCII/Unicode、位/字节)是 AP CSP 大概念 2(DAT-1.A、DAT-1.B)的核心。§6–§7(布尔逻辑、逻辑门)对应 CSTA 3A-DA-09 描述性说明和 3B-CS-02。 Logic gate circuit diagrams (§7) are enrichment for AP CSP students; AP CSP focuses on Boolean expressions rather than drawing physical gates.逻辑门电路图(§7)对 AP CSP 学生为拓展内容;AP CSP 侧重布尔表达式,而非绘制物理门。 CSTA K-12 and AP CSP: CSTA 3A-DA-09, 3B-CS-02; AP CSP Big Idea 2 (DAT) topics 2.1, 2.2:CSTA 3A-DA-09、3B-CS-02;AP CSP 大概念 2(DAT)主题 2.1、2.2
🇨🇦 ON Grade 11: ICS3U安大略 11 年级:ICS3U All seven sections. ICS3U A1.2 explicitly names binary, hex, ASCII, Unicode (§1–§5). ICS3U A1.4 explicitly names AND, OR, NOT (§6–§7). The most direct curriculum match of all four regions.全部 7 节。ICS3U A1.2 明确点名二进制、十六进制、ASCII、Unicode(§1–§5)。ICS3U A1.4 明确点名 AND、OR、NOT(§6–§7)。四个地区中课程匹配度最高。 Nothing: the whole unit falls under ICS3U Strand A.无:整个单元都属于 ICS3U 的 A 部分。 ON/BC Computer Studies 11-12: ICS3U A1.2, A1.4:ICS3U A1.2、A1.4
🇨🇦 BC: CS10 / CIS11BC: CS10 / CIS11 §6 (Boolean logic: AND, OR, NOT) is the strongest curriculum match (CS10 intro-constructs Content). §5 (ASCII/Unicode, bit/byte) connects to CIS11 data-encoding Content. §1–§4 (binary/hex/conversion) are conceptual enrichment; BC has no explicit base-conversion standard.§6(布尔逻辑:AND、OR、NOT)是课程匹配度最高的(CS10 入门构造内容)。§5(ASCII/Unicode、位/字节)与 CIS11 数据编码内容关联。§1–§4(二进制/十六进制/转换)为概念拓展;BC 无明确的进制转换标准。 §7 logic gates are enrichment for BC; BC names Boolean operators as a programming construct, not as circuit-level hardware.§7 逻辑门对 BC 为拓展;BC 将布尔运算符命名为编程构造,而非电路级硬件。 ON/BC Computer Studies 11-12: BC CS10 Boolean operators content bullet; CIS11 data encoding content:BC CS10 布尔运算符内容条目;CIS11 数据编码内容
🇨🇦 AB: CSE1120 / CSE2130阿尔伯塔:CSE1120 / CSE2130 §6 (Boolean operators in programs, CSE1120 outcome 2.6) and §5 (ASCII/binary encoding, CSE2130 outcome 1.1.2) are the matching content. §1–§4 (binary/hex/arithmetic) are enrichment; Alberta CSE has no dedicated number-base module.§6(程序中的布尔运算符,CSE1120 结果 2.6)和 §5(ASCII/二进制编码,CSE2130 结果 1.1.2)是对应的内容。§1–§4(二进制/十六进制/算术)为拓展;阿尔伯塔 CSE 无专门的进制模块。 §7 (logic gates) is enrichment for AB; digital logic / number systems are in other CTS clusters, not CSE.§7(逻辑门)对 AB 为拓展;数字逻辑/数制在其他 CTS 集群中,不在 CSE 中。 Alberta CTS Computing Science: CSE1120 outcomes 2.3, 2.6; CSE2130 outcome 1.1.2:CSE1120 结果 2.3、2.6;CSE2130 结果 1.1.2
!
If you are cramming the night before如果你在临阵磨枪

Memorise five things: how to count in binary and convert a small number to/from decimal; what a bit and byte are; the three Boolean operators AND, OR, NOT and their truth tables; what ASCII is; and how to evaluate a simple Boolean expression. Read every cram-cheat box. Skip the NAND and XOR going-deeper boxes.背熟五件事:如何用二进制计数并将小数字与十进制互转;位和字节是什么;AND、OR、NOT 三个布尔运算符及其真值表;ASCII 是什么;以及如何求一个简单布尔表达式的值。读每个速记框,跳过 §7 深入中关于 NAND 和 XOR 的内容。

*
If you are going for the top mark如果你目标顶分

Master fluent conversion in all three directions (binary ↔ decimal ↔ hex). Be able to construct a full truth table for a compound expression like (A AND B) OR (NOT C). Understand why each binary digit doubles the representable range. Know the Unicode extension story beyond ASCII. AP CSP DAT-1.A and DAT-1.B, and Ontario ICS3U A1.2 + A1.4, are the standards this unit follows.掌握三个方向的流畅转换(二进制 ↔ 十进制 ↔ 十六进制)。能够为复合表达式(如 (A AND B) OR (NOT C))构建完整真值表。理解为何每个二进制位将可表示范围翻倍。了解 ASCII 之外的 Unicode 扩展故事。AP CSP DAT-1.A 和 DAT-1.B 以及安大略 ICS3U A1.2 + A1.4 是本单元遵循的标准。